#include <string.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_ints.h"
#include "driverlib/flash.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/udma.h"
#include "driverlib/systick.h"
#include "driverlib/pin_map.h"
#include "utils/ustdlib.h"
#include "third_party/fatfs/src/ff.h"
#include "third_party/fatfs/src/diskio.h"
#include "drivers/usb_sound.h"
#include "drivers/wavfile.h"
#include "usblib/usblib.h"
#include "audio_play.h"
//#include "drivers/wavfile.h"
#include "ssidac.h"

#define BufferSize 	200
#define first 		0
#define second		1

//Globals
static CONFIG configuration;
//tWavFile audio[16];
unsigned char buffer[16][200];
GRID currentButtons[2];
unsigned short current = first;
/*
 * Loop through an audio file again.
 */
/*void loop(void) {

}*/

void Model(CONFIG config) {
	unsigned short button;
	configuration = config;
	for (button = 0; button < 16; button++) {
		char * filename = (char *)configuration.buttons[button].filename;
		tWavFile audio = configuration.buttons[button].audio;
		WavOpen(filename, &audio);
		configuration.buttons[button].buffer = (unsigned char **)&buffer[button];
	}
}

void PlayAudio(unsigned char firstByte, unsigned char secondByte) {
	SSIDACWrite(firstByte, secondByte);
}

void PlayAudioTimer(void) {
	unsigned int firstLocation = 0;
	unsigned int secondLocation = 0;
	unsigned char firstBuffer[2] = {' ',' '};
	unsigned char secondBuffer[2] = {' ',' '};
	unsigned char currentBuffer[2];
	if (currentButtons[first].enabled != NULL) {
		firstLocation = currentButtons[first].location;
		firstBuffer[0] = *currentButtons[first].buffer[firstLocation];
		firstBuffer[1] = *currentButtons[first].buffer[firstLocation+1];
	}
	if (currentButtons[second].enabled != NULL) {
		secondLocation = currentButtons[second].location;
		secondBuffer[0] = *currentButtons[second].buffer[secondLocation];
		secondBuffer[1] = *currentButtons[second].buffer[secondLocation+1];
	}
	if (firstBuffer[0] == ' ') {
		currentBuffer[0] = secondBuffer[0];
		currentBuffer[1] = secondBuffer[1];
	} else if (secondBuffer[0] == ' ') {
		currentBuffer[0] = firstBuffer[0];
		currentBuffer[1] = firstBuffer[1];
	} else {
		currentBuffer[0] = firstBuffer[0] + secondBuffer[0];
		currentBuffer[1] = firstBuffer[1] + secondBuffer[1];
	}
	if (firstLocation <= 1) {
		WavRead(&currentButtons[first].audio, *currentButtons[second].buffer, BufferSize);
	} else if(secondLocation <= 1) {
		WavRead(&currentButtons[first].audio, *currentButtons[first].buffer, BufferSize);
	}
	PlayAudio(currentBuffer[0], currentBuffer[1]);
}

void PlayButtonSampleUp(unsigned short button) {
	GRID buttonModel = configuration.buttons[button];
	PlayAudio(buffer[button][0], buffer[button][1]);
	if (buttonModel.location >= 200) {
		buttonModel.location = buttonModel.location - 200;
	}
	configuration.buttons[button].location = buttonModel.location + 2;
	if (current == first) {
		current = second;
		currentButtons[second] = configuration.buttons[button];
	} else if(current == second) {
		current = first;
		currentButtons[first] = configuration.buttons[button];
	}
}

/*
 * This takes care of the hatch/latch problem by providing
 * a system to stop the audio if it doesn't need to be
 * played anymore. Needs a better null system.
 */
void PlayButtonSampleDown(unsigned short button) {
	if (configuration.buttons[button].hl == hatch) {
		if (currentButtons[1].button == button) {
			currentButtons[1].enabled = NULL;
			current = first;
			ResetAudio(button);
		} else if(currentButtons[0].button == button) {
			currentButtons[0].enabled = NULL;
			current = second;
			ResetAudio(button);
		}
	}
}

void ResetAudio(unsigned short button) {
	tWavFile audio = configuration.buttons[button].audio;
	WavClose(&audio);
	WavOpen((char *)configuration.buttons[button].filename, &configuration.buttons[button].audio);
}


